<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="./js/vue.js"></script>
    <title>字符串操作方法</title>
  </head>
  <body>
    <script>
      let a = "dfdaf";
      //转大写
      let b = a.toUpperCase();
      console.log(b);
      //转小写
      console.log(b.toLowerCase());
      //找元素索引
      console.log(a.indexOf("f"));
      //切片
      console.log(a.slice(0, 2),'slices');
      console.log(a.slice(0, -1));
      console.log(a.substring(0, 2));
      //去除空白
      let c = " efere ";
      console.log(c);
      console.log(c.trim());
      //分割字符串，放在数组中
      let d = a.split("");
      console.log("d的值为",d);
      //测试包含
      console.log(a.includes("d"));
      //
      let e = "s,d,f, ,d,f";
      console.log(e.trim());
      let f = e.split(",");
      console.log("split方法测试",f);
      let g = "";
      for (let i of f) {
        if (i.trim()) {
          g += i;
        }
      }
      console.log(g);

      function example() {
        let x = 10;
        if (true) {
          // let x = 20;
          console.log(x); // 输出 20
        }
        console.log(x); // 输出 10
      }
    //repeat方法是用来重复字符的
    const a4 = '#';
    console.log('a4',a4.repeat(5));
    
      example();

      //替换字符串中的内容
      let str = "Hello, world!";
      let newStr = str.replace("world", "JavaScript");
      console.log(newStr); // 输出: Hello, JavaScript!
      //使用正则表达式全局替换
      let str1 = "Visit Microsoft! Visit Microsoft!";
      let newStr1 = str1.replace(/Microsoft/g, "W3Schools");
      console.log(newStr1); // 输出: Visit W3Schools! Visit W3Schools!
      //使用正则表达式,替换函数表达式
      let str3 = "Hello 1 world, and 2 people";
      let newStr3 = str3.replace(/\d+/g, function (match) {
        return parseInt(match) * 2;
      });
      console.log(newStr3); // 输出: Hello 2 world, and 4 people
    </script>
  </body>
</html>
